/*
 * Created on Feb 14, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.engine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.query.IPaginator;
import edu.mit.simile.longwell.query.Query;

/**
 * @author dfhuynh
 */
class Paginator implements IPaginator {
	final protected Profile 	m_profile;
	final protected QueryEngine	m_engine;
	final protected Map 		m_paginationCache = new HashMap();

	Paginator(Profile profile, QueryEngine engine) {
		m_profile = profile;
		m_engine = engine;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPaginator#paginate(java.util.List, edu.mit.simile.longwell.query.Query, int, boolean)
	 */
	public List paginate(
		List sortedObjects, Query query, int itemsPerPage, boolean fresh) {
		
		List pages = null;
		
		if (fresh) {
			m_paginationCache.clear();
		} else {
			pages = (List) m_paginationCache.get(sortedObjects);
		}
		
		if (pages == null) {
			pages = new ArrayList();
			
			if (sortedObjects.size() > 0) {
				int totalCount = sortedObjects.size();
				
				int index = 0;
				int pageIndex = 1;
				
				while (index < totalCount) {
					int endIndex = Math.min(index + itemsPerPage, totalCount);
					
					Query query2 = query.dup();
					query2.setParameter(
						"page", Integer.toString(pageIndex - 1));
					
					pages.add(new Page(
						sortedObjects,
						pageIndex,
						index,
						endIndex - index,
						query2
					));
					
					index += itemsPerPage;
					pageIndex++;
				}
				
				m_paginationCache.put(sortedObjects, pages);
			}
		}
		
		return pages;
	}

}
